package leetcode;

import java.util.LinkedList;

/**
 * Created by Ghost on 2014/12/2 0002.
 */
public class ZigZag_Conversion {
    public String convert(String s, int nRows) {
        if(nRows==1)
            return s;
        char[] strs = s.toCharArray();
        LinkedList<Integer> head = new LinkedList<Integer>();
        head.add(0);
        for(int i=1;;i++){
            int h = i*(2*nRows-2);
            if(h<strs.length)
                head.add(h);
            else{
                head.add(h);
                break;
            }
        }
        StringBuilder sb = new StringBuilder("");
        for(int i=0;i<nRows;i++){
            for(Integer idx:head){
                if(idx-i>=0 && idx-i<idx && idx-i<strs.length && i<nRows-1){
                    sb.append(strs[idx-i]);
                }
                if(idx+i>=idx && idx+i<strs.length)
                    sb.append(strs[idx+i]);
            }
        }
        return sb.toString();
    }

    public static void main(String[] args){
        ZigZag_Conversion zigZag_conversion = new ZigZag_Conversion();
        System.out.println(zigZag_conversion.convert("PG",2));
    }
}
